package com.xcj.springai.tools;


import com.xcj.springai.constant.ContentTypeConstants;
import com.xcj.springai.constant.PageEnums;
import com.xcj.springai.constant.ToolContentConstant;
import com.xcj.springai.vo.ChatResponse;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.chat.model.ToolContext;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Component
@RequiredArgsConstructor
@Slf4j
public class G2sSrmTool {


    @Tool(name = "getOverdueTask", description = "查询逾期任务", returnDirect = true)
    public ChatResponse getOverdueTask() {
        ChatResponse.ChatResponseBuilder<Object> chatResponse = ChatResponse.builder().done(true);
        try {
            System.out.println("执行了本地回调的方法");
            HashMap<String, String> map = new HashMap<>();
            map.put("任务A", "逾期超过24小时");
            chatResponse.dt(map).dc("为您查询到以下逾期的任务信息");

        } catch (Exception e) {
            log.error("查询逾期任务时发生异常", e);
            // 捕获异常，设置响应状态为失败，并附上异常信息
            chatResponse.dc("服务器繁忙，请稍后再试！");
        }
        return chatResponse.build();
    }



    @Tool(name = "SupplierDistribution", description = "查询供应商分布", returnDirect = true)
    public ChatResponse SupplierDistribution() {
        ChatResponse.ChatResponseBuilder<Object> chatResponse = ChatResponse.builder().done(true);

        System.out.println("执行了本地回调的方法");
        HashMap<String, String> map = new HashMap<>();
        map.put("中工", "福建厦门");
        chatResponse.dt(map).dc("为您查询到以下供应商");
        return chatResponse.build();
    }

    /**
     * 路由跳转案例
     * @param pageName
     * @param toolContext
     * @return
     */
    @Tool(name = "openPage", description = "用于路由跳转，打开指定页面,当用户输入打开XX页面,返回对应的路由", returnDirect = true)
    public ChatResponse openPage(@ToolParam(description = "页面名称") PageEnums pageName, ToolContext toolContext) {
        Long userId = (Long) toolContext.getContext().get(ToolContentConstant.USER_ID);
        Long tenantId = (Long) toolContext.getContext().get(ToolContentConstant.TENANT_ID);
        Map<String, Object> extraParams = (Map<String, Object>) toolContext.getContext().get(ToolContentConstant.EXTRA_PARAMS);
        ChatResponse.ChatResponseBuilder<Object> chatResponse = ChatResponse.builder().done(true);
        try {
            chatResponse.mt(ContentTypeConstants.OPEN_PAGE_TYPE);
            String pagePath = pageName.getPath();
            String pageLabel = pageName.getLabel();
            String usePath = pagePath;
            String useLabel = pageLabel;
            Map<String, Object> useQuery = null;
            if (extraParams != null && !extraParams.isEmpty()) {
                String path = (String) extraParams.get("path");
                String label = (String) extraParams.get("label");
                Map<String, Object> query = (Map<String, Object>) extraParams.get("query");
                // 如果 path 和 label 都存在且非空，使用它们
                if (path != null && !path.isEmpty() && label != null && !label.isEmpty()) {
                    usePath = path;
                    useLabel = label;
                    useQuery = query;
                }
            }
            Map<String, Object> contentData = new HashMap<>();
            contentData.put("path", usePath);
            contentData.put("label", useLabel);
            if (useQuery != null) {
                contentData.put("query", useQuery);
            }
            chatResponse.dt(contentData).dc("已为您打开页面" + useLabel + "页面,如果有误请重新输入");
        } catch (Exception e) {
            log.error("打开页面失败", e);
            chatResponse.mt(ContentTypeConstants.ERROR_TYPE).dc("抱歉，处理您的请求时出现错误。");
        }
        return chatResponse.build();

    }



    public  record bargainingInfo(@ToolParam(description = "供应商名称") String supplierName, @ToolParam(description = "物料名称") String materialName
            , @ToolParam(description = "物料数量") String materialQuantity, @ToolParam(description = "物料单价") String materialUnitPrice) {
    }
    /**
     * 创建议价单
     */
    @Tool(name = "createSuggestPriceOrder", description = "创建议价单", returnDirect = true)
    public ChatResponse createSuggestPriceOrder(bargainingInfo  info) {
        ChatResponse.ChatResponseBuilder<Object> chatResponse = ChatResponse.builder().done(true);



        ArrayList<String> supplierList = new ArrayList<>();
        supplierList.add("成都致远");

        ArrayList<String> materialList = new ArrayList<>();
        materialList.add("螺母");
        materialList.add("扳手");

        try {
            System.out.println("执行了本地回调的方法");
            HashMap<String, String> map = new HashMap<>();
            map.put("建议价单", "创建成功");
            chatResponse.dt(map).dc("已为您创建建议价单");

        } catch (Exception e) {
            log.error("创建建议价单时发生异常", e);
            // 捕获异常，设置响应状态为失败，并附上异常信息
            chatResponse.dc("服务器繁忙，请稍后再试！");
        }
        return chatResponse.build();
        }

}
